<?php
$sanitize_all_escapes = true;
$fake_register_globals = false;
if (!isset($_REQUEST['medico']))
    session_start();

$landingpage = "index.php?site=" . $_SESSION['site_id'];
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    if (!isset($_REQUEST['medico'])) {
        session_destroy();
        header('Location: ' . $landingpage . '&w');
        exit;
    }
}
$ignoreAuth = true;

include_once('../interface/globals.php');
include_once('../library/acl.inc');


/////////////// FORMA VIEJA
/*
  $query = "SELECT e.pc_eid, e.pc_aid, e.pc_title, e.pc_eventDate, " .
  "e.pc_startTime, e.pc_hometext, u.fname, u.lname, u.mname, " .
  "c.pc_catname " .
  "FROM openemr_postcalendar_events AS e, users AS u, " .
  "openemr_postcalendar_categories AS c WHERE " .
  "e.pc_pid = ? AND e.pc_eventDate >= CURRENT_DATE AND " .
  "u.id = e.pc_aid AND e.pc_catid = c.pc_catid " .
  "ORDER BY e.pc_eventDate, e.pc_startTime";

  //echo $query;

  $res = sqlStatement($query, array($pid));

  echo "<h1>CITAS</h1>";
  if (sqlNumRows($res) > 0) {
  $count = 0;

  while ($row = sqlFetchArray($res)) {
  $count++;
  $dayname = xl(date("l", strtotime($row['pc_eventDate'])));
  $dispampm = "am";
  $disphour = substr($row['pc_startTime'], 0, 2) + 0;
  $dispmin = substr($row['pc_startTime'], 3, 2);
  if ($disphour >= 12) {
  $dispampm = "pm";
  if ($disphour > 12)
  $disphour -= 12;
  }
  if ($row['pc_hometext'] != "") {
  $etitle = 'Comments' . ": " . $row['pc_hometext'] . "\r\n";
  } else {
  $etitle = "";
  }
  if ($GLOBALS['portal_onsite_appt_modify']) {
  echo "<a href='add_edit_event_user.php?eid=" . htmlspecialchars($row['pc_eid'], ENT_QUOTES) .
  "' class='edit_event iframe' title='" . htmlspecialchars($etitle, ENT_QUOTES) . "'>";
  }
  echo "<b>" . htmlspecialchars($dayname . ", " . $row['pc_eventDate'], ENT_NOQUOTES) . "</b><br>";
  echo htmlspecialchars("$disphour:$dispmin " . $dispampm . " " . $row['pc_catname'], ENT_NOQUOTES) . "<br>\n";
  echo htmlspecialchars($row['fname'] . " " . $row['lname'], ENT_NOQUOTES);
  if ($GLOBALS['portal_onsite_appt_modify']) {
  echo "</a><br>\n";
  } else {
  echo "<br>\n";
  }
  }
  if (isset($res) && $res != null) {
  if ($count < 1) {
  echo "&nbsp;&nbsp;" . htmlspecialchars('None', ENT_NOQUOTES);
  }
  }
  } else { // if no appts
  echo htmlspecialchars(xl('No Appointments'), ENT_NOQUOTES);
  }
 */
if (isset($_REQUEST['medico']))
    $pid = $_REQUEST['set_pid'];
$medico = "";
$medicoIni = "";
if (isset($_REQUEST['medico'])) {
    $medico = "&medico=1&set_pid=" . $pid;
    $medicoIni = "?medico=1&set_pid=" . $pid;
}

if (isset($pid) && !$GLOBALS['disable_calendar']) {
    $query = "SELECT e.pc_eid, e.pc_aid, e.pc_title, e.pc_eventDate, " .
            "e.pc_startTime, e.pc_hometext, u.fname, u.lname, u.mname, " .
            "c.pc_catname " .
            "FROM openemr_postcalendar_events AS e, users AS u, " .
            "openemr_postcalendar_categories AS c WHERE " .
            "e.pc_pid = ? AND e.pc_eventDate >= CURRENT_DATE AND " .
            "u.id = e.pc_aid AND e.pc_catid = c.pc_catid " .
            "ORDER BY e.pc_eventDate, e.pc_startTime";

    $res = sqlStatement($query, array($pid));
    
//print_r($res);
    $widgetTitle = xl("Appointments");
    $widgetLabel = "appointments";
    $widgetButtonLabel = xl("Add");
    $widgetButtonLink = "return newEvt();";
    $url = "<a href='#' class='addIcon css_button iframe css_button_left' id='" . $widgetLabel . "' onclick='" . $widgetButtonLink . "' title='" .
            htmlspecialchars($widgetButtonLabel, ENT_NOQUOTES) . "'><i class='icon-plus-sign'></i></a>";
    $widgetButtonLink = "return newEvt();";
    if (sqlNumRows($res) > 0) {
        if ( !acl_check( 'admin', 'adm_cnf' ) ){
            echo "<h2>" . xl("Appointments") .  $url ." <span style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> " . sqlNumRows($res) . " </span></h2>";
        }else{
            echo "<h2>" . xl("Appointments")  ." <span style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> " . sqlNumRows($res) . " </span></h2>";
        }
        ?>
        <ul>
            <?php
            $count = 0;
            while ($row = sqlFetchArray($res)) {
                ?>
                <li>
                    <p>
                        <span class="title"><?php echo htmlspecialchars($row['title'], ENT_NOQUOTES); ?></span>
                        <span class="desc">
                            <?php
                            $count++;
                            $dayname = date("l", strtotime($row['pc_eventDate']));
                            $dispampm = "am";
                            $disphour = substr($row['pc_startTime'], 0, 2) + 0;
                            $dispmin = substr($row['pc_startTime'], 3, 2);
                            if ($disphour >= 12) {
                                $dispampm = "pm";
                                if ($disphour > 12)
                                    $disphour -= 12;
                            }
                            $etitle = xl('(Click to edit)');
                            if ($row['pc_hometext'] != "") {
                                $etitle = xl('Comments') . ": " . ($row['pc_hometext']) . "\r\n" . $etitle;
                            }
                            echo "<a href='javascript:oldEvt(" . htmlspecialchars($row['pc_eid'], ENT_QUOTES) .
                            ")' title='" . htmlspecialchars($etitle, ENT_QUOTES) . "'>";
                            echo "<b>" . htmlspecialchars(xl($dayname) . ", " . $row['pc_eventDate'], ENT_NOQUOTES) . "</b></a>";
                            ?>
                            <?php if( acl_check('admin','root') ){ ?>
                            <a id="borrar<?php echo $row['pc_eid'] ?>" class="enlace_borrar iframe_grafico btnNav fancybox.iframe" title="Borrar" href="<?php echo $web_root; ?>/patients/deleter.php?pc_eid=<?php echo $row['pc_eid'] ?>">
                                <i class="icon-remove"></i>
                            </a>
                            <?php } ?>
                            <?php
                            echo htmlspecialchars("$disphour:$dispmin " . xl($dispampm) . " " . xl_appt_category($row['pc_catname']), ENT_NOQUOTES) . "<br>\n";
                            echo htmlspecialchars($row['fname'] . " " . $row['lname'], ENT_NOQUOTES) . "\n"; // medico
                            //echo htmlspecialchars( $row['date'],ENT_NOQUOTES)."</br>";
                            ?>
                        </span>
                    </p>
                </li>
                <script type="text/javascript" language="JavaScript">

                   $(document).ready(function() {
                        $('#borrar<?php echo $row['pc_eid'] ?>').click(function(e) {
                            e.preventDefault();
                            parent.$.fancybox({
                                href: this.href,
                                autoSize: true,
                                type: 'iframe',
                                helpers: {
                                    overlay: {
                                        opacity: 0.3,
                                        css: {
                                            'background': 'rgba(255, 254, 254,0.85)'
                                        }
                                    } // overlay
                                }, // helpers
                                afterClose: function() {
                                    goPid(<?php echo $pid; ?>);
                                }//afterClose
                            }); // fancybox
                        }); // click
                    });

                </script>
            <?php }
            ?>

        </ul>
        <?php
    } else {
        if ( !acl_check( 'admin', 'adm_cnf' ) ){
            echo "<h2>" . xl("Appointments") . $url ." <span style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> " . sqlNumRows($res) . " </span></h2>";
        }else{
            echo "<h2>" . xl("Appointments") ." <span style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> " . sqlNumRows($res) . " </span></h2>";
        }
        //echo "<ul><li><p>No se han ingresado citas.</p></li></ul>";
    }
}
?>

 <script type="text/javascript" language="JavaScript">

                   $(document).ready(function() {
                            $('#appointments').click(function(e) {
                                e.preventDefault();
                                parent.$.fancybox({
                                    href: this.href,
                                    maxWidth: 800,
                                    maxHeight: 600,
                                    fitToView: false,
                                    type: 'iframe',
                                    iframe: {
                                           preload: false // fixes issue with iframe and IE
                                    },
                                    'scrolling': 'no',
                                    helpers: {
                                        overlay: {
                                            opacity: 0.3,
                                            css: {
                                                'background': 'rgba(255, 254, 254,0.85)',
                                                'overflow': 'hidden'
                                            }
                                        } // overlay
                                    }, // helpers

                                    afterClose: function() {
                                        /*alert(<?php echo $medicoIni; ?>);*/
                                        $("#citas").load("<?php echo $web_root; ?>/patients/get_appointments.php<?php echo $medicoIni; ?>");
                                    }//afterClose
                                }); // fancybox
                            }); // click
                        });
</script>